package com.itheima.reggie.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomerException.class)
    public R<String> hander2(CustomerException ex){
        String message = ex.getMessage();
        return R.error(message);
    }



    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> hander1(SQLIntegrityConstraintViolationException ex){
        String message = ex.getMessage();
        if(message.contains("Duplicate entry")){
            String s = message.split(" ")[2];
            return R.error("账号："+s+"已存在");
        }
        return R.error("未知错误!");
    }


}
